package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.IdRes;

import com.example.androidexample.R;

public class RotateAnimationBuilder implements AnimationBuilder, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    private TextView tvRotateStartAngle;
    private TextView tvRotateEndAngle;
    private float fromDegrees = 0f;
    private float toDegrees = 360f;
    private final AnimationActivity activity;
    private final AnimationPivotParamsProvider pivotParamsBuilder;

    public RotateAnimationBuilder(AnimationActivity activity, AnimationPivotParamsProvider pivotParamsBuilder) {
        this.activity = activity;
        this.pivotParamsBuilder = pivotParamsBuilder;
        resetDegrees();
        initView();
    }

    public void resetDegrees() {
        fromDegrees = 0f;
        toDegrees = 360f;
    }

    private void initView() {
        SeekBar skbRotateStartAngle = activity.findViewById(R.id.skb_rotate_start_angle);
        skbRotateStartAngle.setOnSeekBarChangeListener(this);
        tvRotateStartAngle = activity.findViewById(R.id.tv_rotate_start_angle);

        SeekBar skbRotateEndAngle = activity.findViewById(R.id.skb_rotate_end_angle);
        skbRotateEndAngle.setOnSeekBarChangeListener(this);
        tvRotateEndAngle = activity.findViewById(R.id.tv_rotate_end_angle);

        Button btRotate = activity.findViewById(R.id.bt_rotate);
        btRotate.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        @IdRes
        int seekBarId = seekBar.getId();
        if (seekBarId == R.id.skb_rotate_start_angle) {
            fromDegrees = progress;
            tvRotateStartAngle.setText(fromDegrees +"");
        } else if (seekBarId == R.id.skb_rotate_end_angle) {
            toDegrees = progress;
            tvRotateEndAngle.setText(toDegrees +"");
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onClick(View v) {
        activity.handleAction(AnimationBuilderType.ROTATE, this);
    }

    @Override
    public Animation buildAnimation() {
        return new RotateAnimation(fromDegrees, toDegrees, pivotParamsBuilder.getPivotXType(), pivotParamsBuilder.getPivotXValue(),
                pivotParamsBuilder.getPivotYType(), pivotParamsBuilder.getPivotYValue());

    }
}
